import {createRouter,createWebHashHistory,RouteRecordRaw} from 'vue-router'
import { usePath } from '../stores/path'
import { useLoginStore } from '../stores/loginStore'
export const routes:Array<RouteRecordRaw>=[
    {
        path:'/login',
        component:()=>import('../views/login/login.vue'),
        meta:{
            label:"登录页面",
        }
    },
    {
        path:'/',
        component:()=>import('../layout/index.vue'),
        redirect:'/dataView',
        meta:{label:"首页",icon:"",isShow:false,isLimit:false},
        children:[
            {
                path:'/dataView',
                component:()=>import('../views/dataView/index.vue'),
                meta:{label:"数据视图",icon:"PieChart",isShow:false}
            },
            {
                path:'/sysSet',
                component:()=>import('../views/sysSet/index.vue'),
                meta:{label:"系统设置",icon:"Setting",isShow:true,isLimit:true},
                redirect:'/sysSet/role',
                children:[
                    {
                        path:'/sysSet/role',
                        component:()=>import('../views/sysSet/role.vue'),
                        meta:{label:"角色管理",isShow:true},
                    },
                    {
                        path:'/sysSet/role/setLimit',
                        component:()=>import('../views/sysSet/componets/editRoleLimit.vue'),
                        meta:{label:"角色权限",isShow:false},
                    },
                    {
                        path:'/sysSet/admin',
                        component:()=>import('../views/sysSet/admin.vue'),
                        meta:{label:"管理员管理",isShow:true},
                    },
                    
                    {
                        path:'/sysSet/banner',
                        component:()=>import('../views/sysSet/banner.vue'),
                        meta:{label:"轮播图管理",isShow:true},
                    },
                    {
                        path:'/sysSet/floor',
                        component:()=>import('../views/sysSet/floor.vue'),
                        meta:{label:'楼层管理',isShow:true},
                    }
                ]
            },
            {
                path:'/users',
                component:()=>import('../views/users/index.vue'),
                meta:{label:"读者管理",icon:'User',isShow:true,isLimit:true},
                redirect:'/users/userList',
                children:[
                    {
                        path:'/users/userList',
                        component:()=>import('../views/users/userList.vue'), 
                        meta:{label:"读者管理",icon:"User",isShow:true} 
                    },
                    {
                        path:'/users/userDetail',
                        component:()=>import('../views/users/userDetail.vue'),
                        meta:{label:"读者详情",icon:"User",isShow:false} 
                    },
                    {
                        path:'/users/readerDetail',
                        component:()=>import('../views/users/readerDetail.vue'),
                        meta:{label:"读者详情",icon:"User",isShow:false} 
                    },
                    {
                        path:'/users/userSub',
                        component:()=>import('../views/users/userSub.vue'),
                        meta:{label:"读者申述管理",icon:"User",isShow:false} 
                    }
                    ,
                    {
                        path:'/users/userSubEetail',
                        component:()=>import('../views/users/userSubDetail.vue'),
                        meta:{label:"读者意见详情",icon:"User",isShow:false} 
                    }
                ]
            },
            {
                path:'/borr',
                component:()=>import('../views/borr/index.vue'),
                meta:{label:"借阅管理",icon:'Notebook',isShow:true,isLimit:true},
                redirect:'/borr/brorr',
                children:[
                    {
                        path:'/borr/brorr',
                        component:()=>import('../views/borr/brorr.vue'), 
                        meta:{label:"借阅管理",icon:"Notebook",isShow:true} 
                    },
                    {
                        path:'/borr/renew',
                        component:()=>import('../views/borr/renew.vue'),
                        meta:{label:"续借管理",icon:"Notebook",isShow:false} 
                    }
                ]
            },
            {
                path:'/book',
                component:()=>import('../views/book/index.vue'),
                meta:{label:"图书管理",icon:"Collection",isShow:true,isLimit:true},
                redirect:"/book/bookList",
                children:[
                    {
                        path:"/book/bookList",
                        component:()=>import('../views/book/bookList.vue'),
                        meta:{label:"图书管理",icon:'',isShow:true}
                    },
                    {
                        path:"/book/bookType",
                        component:()=>import('../views/book/bookType.vue'),
                        meta:{label:"图书类型管理",icon:'',isShow:true}
                    },
                    {
                        path:'/book/addBook',
                        component:()=>import('../views/book/addbook.vue')
                    }

                ]
            },
            {
                path:'/seat',
                component:()=>import('../views/seat/index.vue'),
                meta:{label:"座位管理",icon:'DataBoard',isShow:true,isLimit:true},
                redirect:'/seat/seatList',
                children:[
                    {
                        path:'/seat/seatList',
                        component:()=>import('../views/seat/seatList.vue'),
                        meta:{label:'座位列表',icon:'',isShow:true}
                    },
                    {
                        path:'/seat/seatUse',
                        component:()=>import('../views/seat/seatUse.vue'),
                        meta:{label:'座位预约管理',icon:'',isShow:true}
                    },
                    {
                        path:'/seat/address',
                        component:()=>import('../views/seat/address.vue'),
                        meta:{label:'图书馆地点管理',icon:'',isShow:true}
                    },
                    {
                        path:'/seat/seatType',
                        component:()=>import('../views/seat/seatType.vue'),
                        meta:{label:'座位类型管理',icon:'',isShow:true}
                    },
                    
                    
                ]
            },
            {
                path:'/message',
                component:()=>import('../views/msg/index.vue'),
                meta:{label:"消息发布",icon:'Message',isShow:false,isLimit:true},
                redirect:'/message/msgList',
                children:[
                    {
                        path:'/message/msgList',
                        component:()=>import('../views/msg/msgList.vue'),
                        meta:{label:'消息管理',icon:'',isShow:true}
                    },
                    {
                        path:'/message/msg',
                        component:()=>import('../views/msg/msg.vue'),
                        meta:{label:"消息发布",icon:"",isShow:true}
                    }
                ]
            },
            {
                path:'/userInfo',
                component:()=>import('../views/userInfo/index.vue'),
                meta:{label:"个人中心",icon:'',isShow:false,isLimit:false},
                redirect:'/userInfo/edit',
                children:[
                    {
                        path:'/userInfo/edit',
                        component:()=>import('../views/userInfo/userInfo.vue'),
                        meta:{label:"修改信息",icon:'',isShow:false},
                    },
                    {
                        path:'/userInfo/editPasswd',
                        component:()=>import('../views/userInfo/editPasswd.vue'),
                        meta:{label:"密码修改",icon:'',isShow:false},
                    },
                    {
                        path:'/userInfo/loginInfo',
                        component:()=>import('@/views/userInfo/loginInfo.vue'),
                        meta:{label:"登录信息",icon:'',isShow:false},
                    }
                ]
            },
            {
                path:'/noLimit',
                component:()=>import('@/views/noLimit/index.vue'),
                meta:{label:'无权限页面',icon:"",isShow:false},
                children:[
                    {
                        path:'/noLimit',
                        component:()=>import('@/views/noLimit/noLimit.vue'),
                        meta:{label:"无权限",icon:"",isShow:false}
                    }
                ]
            }
        ]
    }
]

const router=createRouter({
    routes,
    history:createWebHashHistory()
})


router.beforeEach((to,from,next)=>{
    let publicAccess:string[]=['/noLimit','/dataView',]
    if(to.path.indexOf('login')==-1){
        const mangerStroe=useLoginStore()
        if(mangerStroe.mangerInfo){
            if(mangerStroe.mangerInfo?.roleList){
                const {authority}=mangerStroe.mangerInfo!.roleList
            let access=authority?.split(",");
            let newAcess=publicAccess.concat(access as string[])
            if(newAcess.includes(to.path)){
                next()
            }else{
                next('/noLimit')
            }
            }
            
        }else{
            next('login')
        }
    }else{
        next()
    }
})

router.beforeResolve(to=>{
    const {getToPath,addTab} =usePath()
    getToPath(to.matched)
    addTab(to.matched[to.matched.length-1])
})
export default router